package com.project;

import com.project.config.ServletConfig;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.Servlet;


public class MainTomCat {
    private  static ApplicationContext springContext = new AnnotationConfigApplicationContext(ServletConfig.class);


    public MainTomCat(){
        Tomcat tomcat = new Tomcat();
        //设置启动端口
        tomcat.setPort(8088);
        //设置http引擎
        tomcat.getConnector();

        //定义上下文
        Context context = tomcat.addContext("", null);
        //注册servlet，第一个参数为上下文对象，第二个参数为servlet名称，第三个参数为servlet对象
        Wrapper w1 = Tomcat.addServlet(context, "findAll", (Servlet) springContext.getBean("findAll"));
        w1.addMapping("/findAll");

        try {
            //启动tomcat
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainTomCat();

    }
}
